package io.gitee.yxsnake.framework.config.jasypt.config;

import io.gitee.yxsnake.framework.config.jasypt.encrypt.Sm4Encryptor;
import io.gitee.yxsnake.framework.config.jasypt.initializer.CustomContextInitializer;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 *
 * @author snake
 * @since 2024/11/16 20:07
 */
@Slf4j
@AutoConfiguration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomEncryptorConfiguration{

    /**
     * sm4 秘钥，可以覆盖配置
     */
    @Value("${sm4.key:e5153bb7469a8edc1853cc0fc6cd0e08}")
    private String sm4Key;

    /**
     * 定义加解密bean名称
     */
    private final static String STRING_ENCRYPTOR_BEAN_NAME = "encryptorBean";

    @PostConstruct
    public void init() {
        log.info("------------ pisces-framework-config-jasypt-starter start information -----------");
        log.info("config-jasypt-starter");
        log.info("-------------------------------------------------------------");
    }

    @Bean(STRING_ENCRYPTOR_BEAN_NAME)
    public StringEncryptor stringEncryptor(){
        return new Sm4Encryptor(sm4Key);
    }

    @Bean
    public CustomContextInitializer customContextInitializer(){
        return new CustomContextInitializer(STRING_ENCRYPTOR_BEAN_NAME,stringEncryptor());
    }


}
